package com.uitls.utils;

import android.Manifest;
import android.app.ActivityManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.pm.PackageManager;
import android.os.Build;
import android.provider.Settings;
import android.support.v4.app.ActivityCompat;
import android.telephony.TelephonyManager;

import java.util.List;
import java.util.UUID;

public class AppUtil {

    /**
     * 如果设置一个app的componentClass为COMPONENT_ENABLED_STATE_DISABLED状态，则不会再launcher的程序图标中发现该app。
     * @param appContext
     * @param componentClass
     * @param enabled
     */
    public static void setEnabledBlocking(Context appContext, Class<?> componentClass,
                                          boolean enabled) {
        PackageManager packageManager = appContext.getApplicationContext().getPackageManager();
        ComponentName componentName = new ComponentName(appContext.getApplicationContext(), componentClass);
        int newState = enabled ? PackageManager.COMPONENT_ENABLED_STATE_ENABLED : PackageManager.COMPONENT_ENABLED_STATE_DISABLED;
        packageManager.setComponentEnabledSetting(componentName, newState,
                PackageManager.DONT_KILL_APP);


    }

    /**
     * 设备唯一码
     * @param context
     * @return
     */
    public static String generateDeviceId(Context context) {
        String deviceId = Settings.Secure.getString(context.getContentResolver(), Settings.Secure.ANDROID_ID);
        if (deviceId == null || "9774d56d682e549c".equals(deviceId)) {
            if (ActivityCompat.checkSelfPermission(context, Manifest.permission.READ_PHONE_STATE) == PackageManager.PERMISSION_GRANTED) {
                TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
                if(telephonyManager != null){
                    deviceId = telephonyManager.getDeviceId();
                }
            }
        }
        if (deviceId == null) {
            deviceId = Build.BOARD + Build.BRAND + Build.CPU_ABI + Build.DEVICE + Build.DISPLAY
                    + Build.FINGERPRINT + Build.HOST + Build.ID + Build.MANUFACTURER + Build.MODEL
                    + Build.PRODUCT + Build.TAGS + Build.TYPE + Build.USER + Build.SERIAL;
        }
        return UUID.nameUUIDFromBytes(deviceId.getBytes()).toString();
    }

    /**
     * 是否后台运行
     * @param context
     * @return
     */
    public static boolean isRunningOnBackground(Context context){
        ActivityManager acm = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        if(acm != null){
            List<ActivityManager.RunningAppProcessInfo> runApps = acm.getRunningAppProcesses();
            if(runApps != null && !runApps.isEmpty()){
                for(ActivityManager.RunningAppProcessInfo app : runApps){
                    if(app.processName.equals(context.getPackageName())){
                        if(app.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_BACKGROUND){
                            return true;
                        }
                    }
                }
            }
        }
        return false;
    }
}
